package 每日一题;

import java.util.Arrays;

public class 从数组中移除最大值和最小值 {
    public int minimumDeletions(int[] nums) {
        int n = nums.length;
        //找到最大最小值的下标
        int minNum = Arrays.stream(nums).min().getAsInt();
        int maxNum = Arrays.stream(nums).max().getAsInt();
        int minIdx=0,maxIdx=0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]==minNum)
                minIdx=i;
            if (nums[i]==maxNum)
                maxIdx=i;
        }
        int x = Math.min(minIdx+1,n-minIdx);
        int y = Math.min(maxIdx+1,n-maxIdx);
        int z = Math.abs(maxIdx-minIdx);
        int[] ints = {x, y, z};
        Arrays.sort(ints);
        return ints[0]+ints[1];
    }
}
